package cetcbigdata.da.service.hainan.util;

import cetcbigdata.da.base.DownloadAndUploadMinio;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import static cetcbigdata.da.base.Base.isNotBlankOrNull;
import static cetcbigdata.da.base.Base.logger;

public abstract class ServiceHNUtil {
    public static JSONObject getListInfo(JSONObject object){
        JSONObject result = new JSONObject();
        result.put("职权名称",object.getString("powername"));
        result.put("所属部门",object.getString("deptname"));
        result.put("职权类别",object.getString("powertype"));
        return result;
    }

    public static JSONObject getSimpleBasicInfo(JSONObject object){
        if (null != object) {
            JSONObject result = new JSONObject();
            result.put("名称", object.getString("subitemName"));
            result.put("事项编码", object.getString("subitemCode"));

            switch (object.getString("administrativeType")){
                case "0":
                    result.put("事项类型","行政检查");
                    break;
                case "1":
                    result.put("事项类型","行政处罚");
                    break;
                case "2":
                    result.put("事项类型","行政强制");
                    break;
                case "3":
                    result.put("事项类型","其他");
                    break;
                default:
                    result.put("事项类型","");

            }

            result.put("行使层级", object.getString("superviseLevelValue"));

            switch (object.getString("superviseMxSxzt")){
                case "0":
                    result.put("事项状态","在用");
                    break;
                case "1":
                    result.put("事项状态","取消");
                    break;
                case "2":
                    result.put("事项状态","暂停");
                    break;
                default:
                    result.put("事项状态", "");

            }
            result.put("实施主体", object.getString("implementBody"));
            result.put("实施主体性质", object.getString("implementNature"));
            switch (object.getString("implementNature")){
                case "01":
                    result.put("实施主体性质","法定机关");
                    break;
                case "02":
                    result.put("实施主体性质","授权组织");
                    break;
                default:
                    result.put("实施主体性质","");

            }
            result.put("实施主体编码", object.getString("organCode"));
            result.put("监管对象类型", object.getString("superviseMxDx"));
            result.put("监管形式", object.getString("superviseFormValue"));
            result.put("监管方式", object.getString("superviseWay"));
            result.put("设定依据", object.getString("setGist"));
            result.put("监管流程", object.getString("superviseFlow"));
            result.put("监管结果", object.getString("superviseResult"));

            return result;
        }
        return null;
    }
    //========================================================================================

    //基本信息
    public static Object getBasicInfo(JSONObject object) {
        JSONObject result = new JSONObject();

        result.put("事项名称", object.getString("taskname"));

        result.put("到现场次数", object.getString("anticipatetype"));
        result.put("承诺办结时限", object.getString("promiseday"));
        JSONObject way = new JSONObject();
        way.put("咨询电话", object.getString("linkway"));
        way.put("投诉电话", object.getString("superviseway"));
        result.put("咨询与监督投诉", way);
        result.put("事项类型", object.getString("tasktypename"));
        result.put("基本编码", object.getString("catalogcode"));
        result.put("实施编码", object.getString("taskcode"));
        result.put("实施主体名称", object.getString("taskcode"));
        result.put("实施主体编码", object.getString("deptcode"));
        result.put("办理地点", object.getString("transactaddr"));
        result.put("咨询方式", object.getString("linkway"));
        result.put("办理时间", object.getString("transacttime"));
        result.put("业务办理项编码", object.getString("taskhandleitem"));
        result.put("行使层级", object.getString("uselevelname"));
        result.put("法定办结时限", object.getInteger("anticipateday") + "工作日");
        result.put("法定办结时限说明", object.getString("anticipateexplain"));

        if (null != object.getString("powersource")) {
            result.put("权力来源", object.getString("powersource").equals("1") ? "法定本级行使" : "无");
        }else {
            result.put("权力来源","无");
        }

        result.put("到办理现场次数",object.getInteger("limitscenenum")+"次");
        result.put("审批模式",object.getString("approvalmodel").equals("1") ? "传统模式" : "不见面模式");
        result.put("委托机构",
                isNotBlankOrNull(object.getString("entrustname")) ?
                        object.getString("entrustname"): "无");
        result.put("联办机构",object.getString("otherdept"));
        result.put("是否收费",object.getString("isfee").equals("1") ? "是":"否");
        result.put("是否支持网上支付",object.getString("isschedule").equals("1") ? "是":"否");
        result.put("是否进驻政务大厅",object.getString("isentrycenter").equals("1") ? "是":"否");
        result.put("特别程序",
                isNotBlankOrNull(object.getString("specialprocedure")) ?
                        object.getString("specialprocedure") : "无");
        result.put("办理形式",object.getString("handletypename"));

        if (null != object.getString("onlinehandledepth")) {
            StringBuilder onlinehandledepthResult = new StringBuilder();
            String onlinehandledepth = object.getString("onlinehandledepth");
            //测试用代码
//            System.out.println(onlinehandledepth);
            char[] onlinehandledepthArray = onlinehandledepth.toCharArray();
            for (char i : onlinehandledepthArray){
                switch (i){
                    case '1':
                        onlinehandledepthResult.append("互联网咨询");
                        break;
                    case '2':
                        onlinehandledepthResult.append("互联网收件");
                        break;
                    case '3':
                        onlinehandledepthResult.append("互联网预审");
                        break;
                    case '4':
                        onlinehandledepthResult.append("互联网受理");
                        break;
                    case '5':
                        onlinehandledepthResult.append("互联网办理");
                        break;
                    case '6':
                        onlinehandledepthResult.append("互联网办理结果信息反馈");
                        break;
                    case '7':
                        onlinehandledepthResult.append("互联网电子证照反馈");
                        break;
                    case '9':
                        onlinehandledepthResult.append("其他 ");
                        break;
                    default:
                        onlinehandledepthResult.append(";");
                }
            }
            result.put("网上办理深度",onlinehandledepthResult);
        }else {
            result.put("网上办理深度","无");
        }

        result.put("监督投诉方式",object.getString("superviseway"));
        result.put("目录来源",object.getString("catalogcode"));
        result.put("实施主体性质",
                object.getString("catalogcode").startsWith("00")?"国家基本目录":
                        object.getString("catalogcode").startsWith("46")?"省级基本目录":"无");
        result.put("承诺办结时限",
                object.getString("projecttype").equals("1")?"当场办结":
                        object.getInteger("promiseday")+"个工作日");
        result.put("承诺办结时限说明",object.getString("promiseexplain"));
        result.put("办件类型",object.getString("projecttypename"));
        result.put("必须现场办理原因说明",object.getString("limitsceneexplain"));

        if (null != object.getString("handlearea")){
            switch (object.getString("handlearea")){
                case "1":
                    result.put("通办范围","全国");
                    break;
                case "2":
                    result.put("通办范围","全省");
                    break;
                case "3":
                    result.put("通办范围","全市");
                    break;
                case "4":
                    result.put("通办范围","全县");
                    break;
                case "5":
                    result.put("通办范围","全镇（乡、街道）");
                    break;
                case "6":
                    result.put("通办范围","跨村（社区）");
                    break;
                default:
                    result.put("通办范围","无");
            }
        }

        try {
            String fileId = object.getString("spjgfileid");
            if (null != fileId && fileId.length() > 0) {
                JSONObject resultname = new JSONObject();
                String sampleResUlt;
                String sampleResultMinioId;
                sampleResUlt = "https://wssp.hainan.gov.cn/hnwt/sx/download?" + "id=" + fileId;
                sampleResultMinioId = fileDownloadAndUploadMinioGetImage("https://wssp.hainan.gov.cn/hnwt/sx/download?" + "id=" + object.getString("spjgfileid"),
                        object.getString("spjgfileid"));
                resultname.put("名称", object.getString("resultname"));
                resultname.put("url", sampleResUlt);
                resultname.put("minioId", sampleResultMinioId);
                result.put("办理结果", resultname);
            }else {
                result.put("办理结果", object.getString("resultname"));
            }
        } catch (Exception e) {
            logger.info(e.toString());
        }

        result.put("是否支持预约办理",object.getString("isschedule").equals("1")?"是":"否");
        result.put("是否支持物流快递",object.getString("isexpress").equals("1")?"是":"否");
        result.put("数量限制",
                isNotBlankOrNull(object.getString("limitexplain")) ?
                        object.getString("limitexplain") : "无");
        result.put("中介服务",object.getString("servicetype"));
        result.put("是否网办",object.getString("isonline").equals("1")?"是":"否");
        result.put("网办地址",object.getString("isonline").equals("1") ? "在线办理" : "无");

        //基本信息.其他信息
        JSONObject jsonDataOI = new JSONObject();
        jsonDataOI.put("事项状态",object.getString("taskstatename"));
        jsonDataOI.put("服务对象",object.getString("servertypename"));
        jsonDataOI.put("移动端是否对接单点登录",object.getString("appissinglelogin").equals("1")?
                "是":"否");
        jsonDataOI.put("计算机端是否对接单点登录",object.getString("issinglelogin").equals("1")?"是":"否");
        jsonDataOI.put("计算机端在线办理跳转地址",object.getString(""));
        jsonDataOI.put("计划生效日期",object.getString("planeffectivedate"));
        jsonDataOI.put("是否支持自助终端办理",object.getString("isserviceterminals").equals("1")?"是":"否");
        jsonDataOI.put("面向自然人事项主题分类",object.getString("usertopictypename"));
        jsonDataOI.put("面向法人事项主题分类",object.getString("corptopictypename"));
        jsonDataOI.put("乡镇街道名称",isNotBlankOrNull(object.getString("townname"))?
                object.getString("townname"):"无");
        jsonDataOI.put("村居社区名称",isNotBlankOrNull(object.getString("villagename"))?
                object.getString("villagename"):"无");
        jsonDataOI.put("事项版本",object.getString("taskversion"));
        jsonDataOI.put("计划取消日期",object.getString("plancanceldate"));
        jsonDataOI.put("四办清单",isNotBlankOrNull(object.getString("pdmstypename")) ?
                object.getString("pdmstypename") : "");
        jsonDataOI.put("面向自然人地方特色主题分类",isNotBlankOrNull(object.getString("usercharactertopictype")) ?
                object.getString("usercharactertopictype"):"无");
        jsonDataOI.put("面向法人地方特色主题分类",isNotBlankOrNull(object.getString("corpcharactertopictype")) ?
                object.getString("corpcharactertopictype"):"无");
        jsonDataOI.put("乡镇街道代码",isNotBlankOrNull(object.getString("towncode"))?
                object.getString("towncode"):"无");
        jsonDataOI.put("村居社区代码",isNotBlankOrNull(object.getString("villagecode"))?
                object.getString("villagecode"):"无");

        result.put("其他信息",jsonDataOI);
        return result;
    }

    //办理材料
    public static Object getMaterialsInfo(JSONArray materials) {
        JSONArray resultList = new JSONArray();

        if (null != materials && materials.size() > 0) {
            for (Object o : materials) {
                JSONObject object = (JSONObject) o;

                //材料名称
                JSONObject materialname = new JSONObject();
                materialname.put("名称", object.getString("materialname"));

                //空白表格
                try {
                    String slybfileid = object.getString("slybfileid");
                    if (null != slybfileid && slybfileid.length() > 0){

                        String emptyFileUrl;
                        String emptyFileMinioId;
                        JSONObject emptyFile = new JSONObject();
                        emptyFileUrl = "https://wssp.hainan.gov.cn/hnwt/sx/download?" + "id=" + slybfileid;
                        emptyFileMinioId = fileDownloadAndUploadMinioGetImage("https://wssp.hainan.gov.cn/hnwt/sx/download?" + "id=" + slybfileid,
                                slybfileid);
                        emptyFile.put("url", emptyFileUrl);
                        emptyFile.put("minioId", emptyFileMinioId);
                        materialname.put("空白表格", emptyFile);
                    }
                } catch (Exception e) {
                    logger.info(e.toString());
                }

                //示例表格
                try {
                    String kbbgfileid = object.getString("kbbgfileid");
                    if (null != kbbgfileid && kbbgfileid.length() > 0) {
                        String sampleFileUrl;
                        String sampleFileMinioId;
                        JSONObject sampleFile = new JSONObject();
                        sampleFileUrl = "https://wssp.hainan.gov.cn/hnwt/sx/download?" + "id=" + kbbgfileid;
                        sampleFileMinioId = fileDownloadAndUploadMinioGetImage("https://wssp.hainan.gov.cn/hnwt/sx/download?" + "id=" + kbbgfileid,
                                kbbgfileid);
                        sampleFile.put("url", sampleFileUrl);
                        sampleFile.put("minioId", sampleFileMinioId);
                        materialname.put("示例详表", sampleFile);
                    }
                } catch (Exception e) {
                    logger.info(e.toString());
                }

                //材料必要性
                JSONObject result = new JSONObject();
                switch (object.getString("isneed")) {
                    case "1":
                        result.put("材料必要性", "必要");
                        break;
                    case "2":
                        result.put("材料必要性", "非必要");
                        break;
                    case "3":
                        result.put("材料必要性", "容缺后补");
                        break;
                    default:
                        result.put("材料必要性", "无");
                }

                //材料要求
                JSONObject materialReq = new JSONObject();
                if (null != object.getString("materialformat")) {
                    switch (object.getString("materialformat")) {
                        case "1":
                            materialReq.put("形式", "纸质");
                            break;
                        case "2":
                            materialReq.put("形式", "电子");
                            break;
                        case "3":
                            materialReq.put("形式", "纸质和电子");
                            break;
                        case "4":
                            materialReq.put("形式", "纸质或电子");
                            break;
                        default:
                            materialReq.put("形式", "无");
                    }
                } else {
                    materialReq.put("形式", "无");
                }
                if (null != object.getString("materialtype")) {
                    switch (object.getString("materialtype")) {
                        case "1":
                            materialReq.put("类型", "原件");
                            break;
                        case "2":
                            materialReq.put("类型", "复印件");
                            break;
                        case "3":
                            materialReq.put("类型", "原件和复印件");
                            break;
                        default:
                            materialReq.put("类型", "无");
                    }
                } else {
                    materialReq.put("类型", "无");
                }
                materialReq.put("份数", object.getInteger("pagenum"));
                materialReq.put("规格", object.getString("pageformat"));
                materialReq.put("填报须知", object.getString("fillexplain"));

                //来源渠道
                JSONObject materialSource = new JSONObject();
                if (null != object.getString("sourcetype")) {
                    switch (object.getString("sourcetype")) {
                        case "10":
                            materialSource.put("来源渠道", "申请人自备");
                            break;
                        case "20":
                            materialSource.put("来源渠道", "政府部门核发");
                            break;
                        case "99":
                            materialSource.put("来源渠道", "其他");
                            break;
                        default:
                            materialSource.put("来源渠道", "其他");
                    }
                } else {
                    materialSource.put("来源渠道", "其他");
                }


                //其他信息
                JSONObject materialOther = new JSONObject();
                materialOther.put("要求提供材料依据", object.getString("bylaw"));
                materialOther.put("受理标准", object.getString("acceptstand"));
                materialOther.put("来源渠道说明", object.getString("sourceexplain"));

                //结果封装
                result.put("材料名称", materialname);
                result.put("材料要求", materialReq);
                result.put("来源渠道", materialSource);
                result.put("其他信息", materialOther);
                resultList.add(result);
            }
            return resultList;
        }
        return "无";
    }

    //办理流程
    public static Object getflowHJsInfo(JSONArray flowHjs, String fileId) {
        JSONObject result = new JSONObject();
        JSONArray jsonDataFlow = new JSONArray();
        int i = 1;
        if (null != flowHjs) {
            for (Object o : flowHjs) {
                JSONObject flowHj = (JSONObject) o;
                JSONObject flowLine = new JSONObject();
                flowLine.put("序号", i);
                flowLine.put("环节名称", flowHj.getString("hjName"));
                flowLine.put("办理人", flowHj.getString("namesShow"));
                flowLine.put("职务", flowHj.getString("positionsShow"));
                flowLine.put("联系方式", flowHj.getString("telephonesShow"));
                flowLine.put("环节办理期限", flowHj.getString("hjLimittime") + "个工作日");
                flowLine.put("环节说明", flowHj.getString("hjSm"));
                jsonDataFlow.add(flowLine);
                i++;
            }
        }else {
            return "无";
        }
        if (null != fileId && fileId.length() > 0) {
            try {
                String minioId;
                String fileUrl;
                fileUrl = "https://wssp.hainan.gov.cn/flow_img/" + fileId + ".png";
                minioId = fileDownloadAndUploadMinioGetImage(fileUrl, fileId + ".png");
                JSONObject fileJson = new JSONObject();
                fileJson.put("minioId", minioId);
                fileJson.put("url", fileUrl);
                result.put("流程信息", jsonDataFlow);
                result.put("文件", fileJson);
            } catch (Exception e) {
                logger.info(e.toString());
            }
        }
        return result;
    }

    //收费信息
    public static Object getFeeInfo(JSONArray feeProjects) {
        if (null != feeProjects && feeProjects.size()>0) {
            JSONArray resultList = new JSONArray();
            for (Object o : feeProjects) {
                JSONObject object = (JSONObject) o;
                JSONObject result = new JSONObject();
                result.put("收费项目名称", object.getString("feename"));
                result.put("收费标准", object.getString("feestand"));
                result.put("是否允许减免", object.getString("isdesc").equals("1") ? "是" : "否");
                result.put("允许减免依据", object.getString("descexplain"));
                result.put("备注", object.getString("remark"));
                resultList.add(result);
            }
            return resultList;
        }
        return "无收费内容";
    }


    //常见问题
    public static Object getQuestionInfo(JSONArray questions){
        if (null != questions && questions.size()>0){
            JSONArray resultList = new JSONArray();
            for (Object o : questions){
                JSONObject question = (JSONObject) o;
                JSONObject result = new JSONObject();
                result.put("问题",question.getString("question"));
                result.put("回答",question.getString("answer"));

                resultList.add(result);
            }
            return resultList;
        }
        return "无";
    }

    public static String fileDownloadAndUploadMinioGetImage(String url, String fileName ) throws Exception {
        JSONObject param = new JSONObject();
        JSONObject headers = new JSONObject();
        headers.put("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36");

        //=========================================================测试性注释以下代码===============================================================
        return DownloadAndUploadMinio.getInstance().downloadGet(
                url,
                headers,
                param,
                fileName
        );
//        return null;
    }


}
